import { Component, OnInit } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { NzMessageService } from 'ng-zorro-antd';
import { Operator } from '@core/config/dic.config';
import { UtilService } from '@core/services/util.service';
import { AreaService } from '../../../sys-setting/area-manage/area.service';
@Component({
  selector: 'app-ptinfo-module',
  templateUrl: './ptinfo.component.html',
  styleUrls: ['./index.less']
})
export class PtinfoModuleComponent implements OnInit {
  searchValue: '';


  constructor(private http: _HttpClient, private modal: ModalHelper, private api: ApiService, private msg: NzMessageService, private util: UtilService,
              private area: AreaService
              ) { }
  searchSchema: SFSchema = {
    properties: {
    }
  };
  navData = []
  public tableHeader: any[] = [
    {
      title: "所属村",
      index: "areaName",
      filter: {
        placeholder: '所属村',
        type: 'string'
      }
    },
    {
      title: "房屋名称",
      index: "houseName",
      filter: {
        placeholder: '请输入房屋名称',
        type: 'string'
      }
    }, {
      title: "门名称",
      index: "doorName",
      filter: {
        placeholder: '门名称',
        type: 'string'
      }
    }, {
      title: "设备名称",
      index: "deviceName",
      filter: {
        placeholder: '请输入设备名称',
        type: 'string'
      }
    }, {
      title: "主机串号",
      index: "deviceCode",
      filter: {
        placeholder: '请输入主机串号',
        type: 'string'
      }
    }, {
      title: "所属运营商",
      index: "deviceOperator",
      filter: {
        placeholder: '请选择运营商',
        type: 'select',
        items: Operator
      }
    }, {
      title: "实时状态",
      index: "deviceStatus",
      filter: {
        placeholder: '请选择状态',
        type: 'select',
        items: [
          { label: '在线', value: '0' },
          { label: '离线', value: '1' }
        ]
      }
    }, {
      title: "最近上线时间",
      index: "onlineTime",
      filter: {
        placeholder: '请输入上线时间',
        type: 'string'
      }
    },
    {
      title: "最近离线时间",
      index: "offTime",
      filter: {
        placeholder: '请输入离线时间',
        type: 'string'
      }
    },
    {
      title: "离线时长",
      index: "offHour",
      filter: {
        placeholder: '请输入离线时长',
        type: 'string'
      }
    }, {
      title: "运行情况",
      index: "runCase"
    }
  ]
  users =
  [
    // {
    //   "houseId": "333",
    //   "areaName": "梅林村",
    //   "houseName": "梅林村333号",
    //   "doorName": "正门",
    //   "devices": [
    //     {
    //       "id": "647103795418509312",
    //       "deviceName": "侧门摄像头",
    //       "deviceCode": "111",
    //       "deviceOperator": 0,
    //       "deviceStatus": 0,
    //       "onlineTime": "2019-11-23 23:31:06",
    //       "offTime": "2019-11-22 17:51:33",
    //       "offHour": "70.37"
    //     },
    //     {
    //       "id": "647103795418509312",
    //       "deviceName": "正门摄像头",
    //       "deviceCode": "111",
    //       "deviceOperator": 0,
    //       "deviceStatus": 0,
    //       "onlineTime": "2019-11-23 23:31:06",
    //       "offTime": "2019-11-22 17:51:33",
    //       "offHour": "20.37"
    //     },
    //     {
    //       "id": "647103877459095552",
    //       "deviceName": "正门摄像头",
    //       "deviceCode": "777",
    //       "deviceOperator": null,
    //       "deviceStatus": 0,
    //       "onlineTime": null,
    //       "offTime": "2019-11-20 14:56:33",
    //       "offHour": "121.29"
    //     }
    //   ],
    //   "openDoorCount": 0,
    //   "captureCount": 1
    // }
  ];
  listOfDisplayData = []
  param = {
    pageSize: 10,
    pageNum: 1,
    total: 0
  }
  operatorDic = this.util.getObjDic(Operator);
  location$; // 监听选择区域
  ngOnInit() {
    // this.setConfig();
    this.getTableDataList();
    this.getStatData();
    this.location$ = this.area.getLocation().subscribe(res => {
      this.currentPageDataChange(1);
      this.getStatData();
    })
  }
  searchData(e): void {
    // tslint:disable-next-line: forin
    for (const key in e) {
      const arrData = this.users.filter((item) => {
        if (item[key].indexOf(e[key]) !== -1) {
          return item;
        }
      })
      // this.users = arrData
    }
  }
  // 获取统计数据方法
  getStatData() {
    this.http
      .post(this.api.assess.statistics.url, {
        // areaId: ''
      }).subscribe((res) => {
        // console.log(res);

        const { status, data, message } = res;
        if (status === '200') {
          this.navData = [{
            count: data.houseCount,
            name: '房屋总数'
          }, {
            count: data.installCount,
            name: '安装设备套数'
          }, {
            count: data.onlineCount,
            name: '当前在线数'
          }, {
            count: data.offlineCount,
            name: '当前离线数'
          }, {
            count: data.onlineRate + '%',
            name: '门禁在线率'
          }, {
            count: data.over24hCount,
            name: '离线超过24小时'
          }]
        } else if (status === 400) {
          this.msg.error(message)
        }
      })
  }
  // 获取table列表数据
  getTableDataList() {
    this.http.post(this.api.device.pointPage.url, {pageSize: 10, pageNum: this.param.pageNum}).subscribe((res) => {
      const { status, data, message } = res;
      if (status === '200') {
        this.users = data.list;
        this.param.total = data.total;
      } else{
        this.msg.error(message);
      }
    })
  }
  // 重置
  reset(value: string) {

  }
  // 搜索
  search(value: string) {

  }
  setConfig() {
    this.tableHeader.map(item => {
      if (item.filter) {
        this.searchSchema.properties[item.filter.index || item.index] = {
          type: 'string',
          title: ''
        }
        this.searchSchema.properties[item.filter.index || item.index].ui = {
          widget: item.filter.type,
          placeholder: item.filter.placeholder || item.title,
          mode: item.filter.mode,
          width: 200
        }
        if (item.filter.items) {
          this.searchSchema.properties[item.filter.index || item.index].enum = item.filter.items;
        }
      }
      const { filter, ...obj } = item;
      return obj;
    })
  }
  currentPageDataChange(e): void {
    this.param.pageNum = e;
    this.getTableDataList();
  }
}
